package com.andersonlfeitosa.encryptor.constants;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import com.andersonlfeitosa.encryptor.exceptions.OperationNotFoundException;
import com.andersonlfeitosa.encryptor.functions.Decoder;
import com.andersonlfeitosa.encryptor.functions.Encoder;

public enum Operation {
	
	ENCODE {
		
		public String execute(String text) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
			return Encoder.encode(text);
		}
		
	},
	
	DECODE {
		
		public String execute(String text) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
			return Decoder.decode(text);
		}
		
	};
	
	/**
	 * Return a operation of the specified argument.
	 * @param op A name of operation.
	 * @return A operation.
	 * @throws OperationNotFoundException If a argument isn't a operation or is null.
	 */
	public static Operation createOperation(String op) throws OperationNotFoundException {
		Operation o = null;
		
		try {
			o = Operation.valueOf(op.toUpperCase());
		} catch (IllegalArgumentException e) {
			throw new OperationNotFoundException();
		} catch (NullPointerException e) {
			throw new OperationNotFoundException();
		}
		
		return o;
	}
	
	/**
	 * Return a text or hash of specified argument.
	 * 
	 * @param text or hash to encode or decode.
	 * @return a text or hash of specified argument.
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public abstract String execute(String text) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException;

}
